﻿using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;


namespace BLL
{
    public abstract class BaseBLL<T>:IBaseBLL<T> where T:class,new()
    {
        #region 架构信息
        public BaseBLL()
        {
            SetDAL();
        }
        public IBaseDAL<T> idal;

        public abstract void SetDAL();

        private IDBSession iDbSession;
        
        public IDBSession DBSession
        {
            get
            {
                if (iDbSession == null)
                {
                    IDBSessionFactory DBSFactory = DI.Spring.GetObject<IDBSessionFactory>("DBSessFactory");
                    iDbSession = DBSFactory.GetDBSession();
                }
                return iDbSession;
            }
        }

        
        #endregion
     


        public virtual void ADD(T model)
        {
             idal.ADD(model);
        }

        public virtual void Del(T model)
        {
             idal.Del(model);
        }

        public virtual void DelByWhere(Expression<Func<T, bool>> delwhere)
        {
             idal.DelByWhere(delwhere);
        }

        public virtual void ModifyIsNullADD(T model, Expression<Func<T, bool>> whereLambda, bool isAll = false, params string[] ModifiedProNames)
        {
            idal.ModifyIsNullADD(model, whereLambda, isAll, ModifiedProNames);
        }

        public virtual void Modify(T model, params string[] ProNames)
        {
             idal.Modify(model, ProNames);
        }

        public virtual void Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] ModifiedProNames)
        {
             idal.Modify(model, whereLambda, ModifiedProNames);
        }

        public virtual List<T> GetListByWhere(Expression<Func<T, bool>> whereLambda)
        {
            return idal.GetListByWhere(whereLambda);
        }

        public virtual List<T> GetListByWhere<K>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, K>> orderLambda)
        {
            return idal.GetListByWhere(whereLambda, orderLambda);
        }

        public virtual List<T> GetPageList<K>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, K>> orderLambda)
        {
            return idal.GetPageList(pageIndex, pageSize, whereLambda, orderLambda);
        }

        public virtual List<T> GetPagedList<K>(int pageIndex, int pageSize, ref int rowCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, K>> orderBy, bool isAsc = true)
        {
            return idal.GetPagedList(pageIndex, pageSize, ref rowCount, whereLambda, orderBy, true);
        }

        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="strSql"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public virtual int ExecuteSqlCommand(string strSql, params object[] paras)
        {
            return idal.ExecuteSqlCommand(strSql, paras);
        }

        public virtual object SqlQuery(string sql, params object[] paras)
        {
            return idal.SqlQuery(sql, paras);
        }

        public virtual List<T> ExecProcedure<T>(string functionName, params ObjectParameter[] parameter)
        {
            return idal.ExecProcedure<T>(functionName, parameter);
        } 

    }
}
